﻿using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using BLL.Services.Authors;
using Web.Infrastructure.Models;

namespace Web.Areas.Admin.Models.Authors
{
    public class AuthorEditModel : BaseModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AuthorEditModel" /> class.
        /// </summary>
        /// <param name="author">The author.</param>
        public AuthorEditModel(Author author)
        {
            ToModel(author, this);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AuthorEditModel" /> class.
        /// </summary>
        public AuthorEditModel()
        {
        }

        /// <summary>
        /// Gets or sets the author id.
        /// </summary>
        /// <value>
        /// The author id.
        /// </value>
        [UIHint("hidden")]
        [Remote("Validate", "Authors", "Admin", AdditionalFields = "FirstName,LastName,MiddleName", ErrorMessage = "Author with the same first name, last name and middle name already exists.")]
        public int AuthorId { get; set; }

        /// <summary>
        /// Gets or sets the first name.
        /// </summary>
        /// <value>
        /// The first name.
        /// </value>
        [Display(Name = "First name")]
        [StringLength(256)]
        [Required]
        [Remote("Validate", "Authors", "Admin", AdditionalFields = "AuthorId,LastName,MiddleName", ErrorMessage = "Author with the same first name, last name and middle name already exists.")]
        public string FirstName { get; set; }

        /// <summary>
        /// Gets or sets the last name.
        /// </summary>
        /// <value>
        /// The last name.
        /// </value>
        [Display(Name = "Last name")]
        [StringLength(256)]
        [Required]
        [Remote("Validate", "Authors", "Admin", AdditionalFields = "AuthorId,FirstName,MiddleName", ErrorMessage = "Author with the same first name, last name and middle name already exists.")]
        public string LastName { get; set; }

        [Display(Name = "Middle name")]
        [StringLength(256)]
        [Remote("Validate", "Authors", "Admin", AdditionalFields = "AuthorId,FirstName,LastName", ErrorMessage = "Author with the same first name, last name and middle name already exists.")]
        public string MiddleName { get; set; }

        /// <summary>
        /// Authors the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public static explicit operator Author(AuthorEditModel model)
        {
            var author = new Author();
            ToEntity(model, author);
            return author;
        }
    }
}